package service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import dao.StudentDAO;
import vo.Student;

public class StudentService {
	private Scanner sc=new Scanner(System.in);
	private BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	private StudentDAO studentDAO=new StudentDAO();
	private List<Student> list=new ArrayList<>();
	
	public void showMenu() {
		System.out.println();
		System.out.println("1 显示");
		System.out.println("2 新增");
		System.out.println("3 修改");
		System.out.println("4 删除");
		System.out.println("5 排序");
		System.out.println("6 保存");
		System.out.println("7 读取");
		System.out.println("0 退出");
		System.out.print("请选择：");
	}

	public int readInt() {
		return sc.nextInt();
	}

	public void print() {
		System.out.println("编号\t学号\t姓名\t语文\t数学\t英语\t总分");
		for (Student s : list) {
			System.out.println(s.getId()+"\t"+s.getStuNo()+"\t"+s.getName()+"\t"+s.getChinese()+"\t"+s.getMath()+"\t"+s.getEnglish()+"\t"+s.sum());
		}
		
	}

	public void add() {
		Student s=readStudent();
		studentDAO.save(s);
		list.add(s);
	}

	private Student readStudent() {
		Student s=new Student();
		try {
			System.out.print("学号:");
			s.setStuNo(br.readLine());
			System.out.print("姓名:");
			s.setName(br.readLine());
			System.out.print("语文:");
			s.setChinese(sc.nextInt());
			System.out.print("数学:");
			s.setMath(sc.nextInt());
			System.out.print("英语:");
			s.setEnglish(sc.nextInt());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return s;
	}

	public void modify() {
		System.out.print("请输入要修改的学生的学号：");
		String stuNo=sc.next();
		Student student=null;
		for (Student s : list) {
			if(stuNo.equals(s.getStuNo())){
				student=s;
				break;
			}
		}
		if(student==null){
			System.out.println("没有找到这个学生");
			return;
		}
		System.out.print("学号:"+student.getStuNo()+"  修改为：");
		String str;
		try {
			str = br.readLine();
			if(str!=null&&!"".equals(str)){
				student.setStuNo(str);
			}
			System.out.print("姓名:"+student.getName()+"  修改为：");
			str=br.readLine();
			if(str!=null&&!"".equals(str)){
				student.setName(str);
			}	
			System.out.print("语文:"+student.getChinese()+"  修改为：");
			str=br.readLine();
			if(str!=null&&!"".equals(str)){
				try {
					int xx=Integer.parseInt(str);
					student.setChinese(xx);
				} catch (Exception e) {
					System.out.println("输入错误");
					return;
				}
			}
			
			System.out.print("数学:"+student.getMath()+"  修改为：");
			str=br.readLine();
			if(str!=null&&!"".equals(str)){
				try {
					int xx=Integer.parseInt(str);
					student.setMath(xx);
				} catch (Exception e) {
					System.out.println("输入错误");
					return;
				}
			}
			System.out.print("英语:"+student.getEnglish()+"  修改为：");
			str=br.readLine();
			if(str!=null&&!"".equals(str)){
				try {
					int xx=Integer.parseInt(str);
					student.setEnglish(xx);
				} catch (Exception e) {
					System.out.println("输入错误");
					return;
				}
			}
			studentDAO.save(student);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	
	
	
}
